package com.roger.myHome.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.roger.myHome.importExcel.InvoiceImport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;

@Slf4j
public class ExcelUtil {

    public static List<InvoiceImport> readFile(MultipartFile file) throws Exception{
        List<InvoiceImport> orderList = new ArrayList<>();
        EasyExcel.read(file.getInputStream(), InvoiceImport.class,new AnalysisEventListener<InvoiceImport>() {
            @Override
            public void invoke(InvoiceImport manHoursApproval, AnalysisContext arg1) {
                // 读取每条数据
                orderList.add(manHoursApproval);
            }

            @Override
            public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
                // 读取到列头
                log.info("{}" ,headMap);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext arg0) {
                // 读取完毕
                log.info("END");
            }
        }).sheet().doRead();
        return orderList;
    }

}
